package socket.ip;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.UnknownHostException;

/**
 * @author 周宇
 * @create 2020-08-18 16:48
 */
public class InetAddressTest {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress1 = InetAddress.getByName("192.168.1.68");
            System.out.println(inetAddress1);

            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2); //域名解析

            //获取本地IP
            InetAddress inetAddress3 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress3);
            InetAddress inetAddress4 = InetAddress.getByName("localhost");
            System.out.println(inetAddress4);
            InetAddress inetAddress5 = InetAddress.getLocalHost();
            System.out.println(inetAddress5);

            //getHostName
            System.out.println(inetAddress2.getHostName());
            //getHostAddress
            System.out.println(inetAddress2.getHostAddress());
            //Canonical : 规范的
            System.out.println(inetAddress2.getCanonicalHostName());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
